package com.dys;

import com.dys.dao.PersonDAO;
import com.dys.dao.impl.PersonDaoImpl;
import com.dys.entity.Person;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: com.dys.DaoTest
 * @description: 测试DAO接口
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/12 下午11:01
 */
public class DaoTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws SQLException {
        testFindAll();
    }

    /**
     * 测试添加
     *
     * @throws SQLException
     */
    private static void testInsert() throws SQLException {
        PersonDAO p = new PersonDaoImpl();
        Person person = new Person();
        person.setName("赵丽颖");
        person.setAge(28);
        person.setEmail("123@qq.com");
        person.setDateOfBirth(new Date(System.currentTimeMillis()));
        person.setAddress("广东深圳龙岗区布吉街道");
        int count = p.insert(person);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 测试修改
     *
     * @throws SQLException
     */
    private static void testUpdate() throws SQLException {
        PersonDAO p = new PersonDaoImpl();
        Person person = new Person();
        person.setId(1);
        person.setName("关晓彤");
        person.setAge(25);
        person.setDateOfBirth(new Date(System.currentTimeMillis()));
        int count = p.updateById(person);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 测试删除
     *
     * @throws SQLException
     */
    private static void testDelete() throws SQLException {
        PersonDAO p = new PersonDaoImpl();
        int count = p.deleteById(2);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 测试获取单个对象
     *
     * @throws SQLException
     */
    private static void testFindById() throws SQLException {
        PersonDAO p = new PersonDaoImpl();
        Person person = p.findById(1);
        System.out.println("查询结果：" + person);
    }

    /**
     * 查询所有
     *
     * @throws SQLException
     */
    private static void testFindAll() throws SQLException {
        PersonDAO p = new PersonDaoImpl();
        List<Person> list = p.findAll();
        list.stream().forEach(System.out::println);
    }
}
